<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 14:34
 */

namespace app\service;


use app\model\CardsModel;
use think\Exception;

/**
 * 名片
 * Class CardService
 * @package app\service
 */
class CardsService extends BaseService
{
    public $cardModel;
    public function __construct()
    {
        $this->cardModel=new CardsModel();
    }



    /**
     * 添加名片
     * @param $add
     * @return bool|false|int
     */
    public function addCard($add){
        try{
           return $this->cardModel->insertGetId($add);
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    /**
     * 编辑名片
     * @param $data
     * @param $where
     * @return bool|false|int
     */
    public function editCard($data,$where){
        try{
            return $this->cardModel->save($data,$where);
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    /**
     * 名片详情
     * @param $where
     * @param $field
     * @return array|bool|false|\PDOStatement|string|\think\Model
     */
    public function findCard($where,$field='a.*,b.name as professional_name'){

        try{
            $data = $this->cardModel
                ->alias('a')
                ->join('zkhb_professional b','a.professional = b.id','left')
                ->field($field)
                ->where($where)
                ->find();
            if($data){
                $data = $data->toArray();
                $data['headimg'] = tomedia($data['headimg']);
                $picture = json_decode($data['pictures'],true);
                $pictures = [];
                if($picture){
                    foreach ($picture as $k => $v){
                        $pictures[$k] = tomedia($v);
                    }
                }
                $data['pictures'] = $pictures;
            }
            return $data;
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }



}